import { createWebHistory, createRouter } from "vue-router";
import Layout from "@/layout";
// import {loadView} from '@/store/modules/permission'
import { treeData } from '@/views/businessManage/common/propertyTree'
import { treeDataSmall } from '@/views/businessManage/common/intergrateTree'

/**
 * Note: 路由配置项
 *
 * hidden: true                     // 当设置 true 的时候该路由不会再侧边栏出现 如401，login等页面，或者如一些编辑页面/edit/1
 * alwaysShow: true                 // 当你一个路由下面的 children 声明的路由大于1个时，自动会变成嵌套的模式--如组件页面
 *                                  // 只有一个时，会将那个子路由当做根路由显示在侧边栏--如引导页面
 *                                  // 若你想不管路由下面的 children 声明的个数都显示你的根路由
 *                                  // 你可以设置 alwaysShow: true，这样它就会忽略之前定义的规则，一直显示根路由
 *                                  // 该参数功能已被删除
 * redirect: noRedirect             // 当设置 noRedirect 的时候该路由在面包屑导航中不可被点击
 * name:'router-name'               // 设定路由的名字，一定要填写不然使用<keep-alive>时会出现各种问题
 * query: '{"id": 1, "name": "ry"}' // 访问路由的默认传递参数
 * meta : {
    noCache: true                   // 如果设置为true，则不会被 <keep-alive> 缓存(默认 false)
    title: 'title'                  // 设置该路由在侧边栏和面包屑中展示的名字
    icon: 'svg-name'                // 设置该路由的图标，对应路径src/assets/icons/svg
    breadcrumb: false               // 如果设置为false，则不会在breadcrumb面包屑中显示
    activeMenu: '/system/user'      // 当路由设置了该属性，则会高亮相对应的侧边栏。
  }
 */
const modules = import.meta.glob("@/views/**/*.vue");
const loadView = (view) => {
  let res;
  for (const path in modules) {
    const dir = path.split("views/")[1].split(".vue")[0];
    if (dir === view) {
      res = () => modules[path]();
    }
  }
  return res;
}

let spatialDataChild = []
for (let i = 1; i <= 46; i++) {
  spatialDataChild.push({
    path: `Pt${i}`,
    name: `Pt${i}`,
    component: loadView('businessManage/dataLoader/subject/spatialData/detailForm/point/Pt' + i)
  })
}
for (let i = 1; i <= 48; i++) {
  spatialDataChild.push({
    path: `L${i}`,
    name: `L${i}`,
    component: loadView('businessManage/dataLoader/subject/spatialData/detailForm/line/L' + i)
  })
}
for (let i = 1; i <= 46; i++) {
  spatialDataChild.push({
    path: `P${i}`,
    name: `P${i}`,
    component: loadView('businessManage/dataLoader/subject/spatialData/detailForm/surface/P' + i)
  })
}
let otherDataChild = []
treeData.map(item => {
  item.children.map(itemChild => {
    otherDataChild.push({
      path: itemChild.pathName,
      name: itemChild.pathName,
      component: loadView(`businessManage/dataLoader/subject/otherData/detailForm/${itemChild.pathName}`)
    })
  })
})

let otherDataSmallChild = []
treeDataSmall.map(item => {
  item.children.map(itemChild => {
    let component
    if (itemChild.pathName.startsWith('Pt')) {
      component = loadView(`businessManage/dataLoader/subject/spatialData/detailForm/point/${itemChild.pathName}`)
    } else if (itemChild.pathName.startsWith('P')) {
      component = loadView(`businessManage/dataLoader/subject/spatialData/detailForm/surface/${itemChild.pathName}`)
    } else if (itemChild.pathName.startsWith('L')) {
      component = loadView(`businessManage/dataLoader/subject/spatialData/detailForm/line/${itemChild.pathName}`)
    } else {
      component = loadView(`businessManage/dataLoader/subject/otherData/detailForm/${itemChild.pathName}`)
    }

    otherDataSmallChild.push({
      path: itemChild.pathName,
      name: 'dataIntegrate-' + itemChild.pathName,
      component
    })
  })
})


// 公共路由
export const constantRoutes = [
  {
    path: "/redirect",
    component: Layout,
    hidden: true,
    children: [
      {
        path: "/redirect/:path(.*)",
        component: () => import("@/views/redirect/index.vue"),
      },
    ],
  },
  {
    path: "/:pathMatch(.*)*",
    component: () => import("@/views/error/404"),
    hidden: true,
  },
  {
    path: "/401",
    component: () => import("@/views/error/401"),
    hidden: true,
  },
  {
    path: "/",
    component: Layout,
    redirect: "/index",
    children: [
      {
        path: "index",
        component: () => import("@/views/index"),
        name: "Index",
        meta: { title: "活动断层成果展示", icon: "fa-home-lg-alt", affix: true },
      },
    ],
  },
  {
    path: "/achievementSearch",
    component: Layout,
    hidden: true,
    redirect: "noRedirect",
    meta: { title: "活动断层与探察成果分类检索" },
    children: [
      {
        path: "index",
        component: () => import("@/views/achievementSearch/index"),
        name: "achievementSearch",
        meta: { title: "活动断层与探察成果分类检索", noCache: true },
        children: [...spatialDataChild, ...otherDataChild].map(item => {
          return {...item, name: `achievementSearch-${item.name}`}
        })
      }
    ]
  },
  {
    path: "/projectManage",
    component: Layout,
    hidden: true,
    redirect: "noRedirect",
    meta: { title: "项目管理" },
    children: [
      {
        path: "project",
        component: () => import("@/views/projectManage/project/index"),
        name: "project",
        meta: { title: "项目管理", noCache: true }
      },
      {
        path: "subject",
        component: () => import("@/views/projectManage/subject/index"),
        name: "subject",
        meta: { title: "专题管理", noCache: true },
      }
    ]
  },
  {
    path: "/businessManage",
    component: Layout,
    hidden: true,
    redirect: "noRedirect",
    meta: { title: "业务管理" },
    children: [
      {
        path: "dataLoader",
        component: () => import("@/views/businessManage/dataLoader/index"),
        name: "dataLoader",
        meta: { title: "数据入库", noCache: true },
        children: [
          {
            path: "project",
            component: () => import("@/views/businessManage/dataLoader/project"),
            name: "dataLoaderProject",
            meta: { noCache: true }
          },
          {
            path: "subject",
            component: () => import("@/views/businessManage/dataLoader/subject"),
            name: "dataLoaderSubject",
            redirect: '/businessManage/dataLoader/subject/spatialData',
            meta: { noCache: true },
            children: [
              {
                path: 'spatialData',
                name: 'spatialData',
                component: () => import("@/views/businessManage/dataLoader/subject/spatialData/index"),
                children: spatialDataChild
              },
              {
                path: 'otherData',
                name: 'otherData',
                component: () => import("@/views/businessManage/dataLoader/subject/otherData"),
                children: otherDataChild
              }
            ]
          }
        ]
      },
      {
        path: "dataIntegrate",
        component: () => import("@/views/businessManage/dataIntegrate/index"),
        name: "dataIntegrate",
        meta: { title: "数据检测集成", noCache: true },
        children: [
          {
            path: 'main',
            name: 'main',
            component: () => import("@/views/businessManage/dataIntegrate/main"),
            children: otherDataSmallChild
          }
        ]
      }
    ]
  },
  {
    path: "/quickMapping",
    component: Layout,
    hidden: true,
    redirect: "noRedirect",
    meta: { title: "地震应急快速出图" },
    children: [
      {
        path: "index",
        component: () => import("@/views/quickMapping/quickMapping/index"),
        name: "quickMapping",
        meta: { title: "地震应急快速出图", noCache: true },
        children: [...spatialDataChild].map(item => {
          return {...item, name: `quickMapping-${item.name}`}
        })
      },
      {
        path: "achievementReview",
        component: () => import("@/views/quickMapping/achievementReview/index"),
        name: "achievementReview",
        meta: { title: "快速出图成果审核", noCache: true }
      },
      {
        path: "dataManage",
        component: () => import("@/views/quickMapping/dataManage/index"),
        name: "dataManage",
        meta: { title: "数据管理", noCache: true }
      }
    ]
  },
  {
    path: "/knowledgeManage",
    component: Layout,
    hidden: true,
    redirect: "noRedirect",
    meta: { title: "活动断层知识库管理" },
    children: [
      {
        path: "knowledgeBase",
        component: () => import("@/views/knowledgeManage/knowledgeBase/index"),
        name: "knowledgeBase",
        meta: { title: "知识库管理", noCache: true }
      },
      {
        path: "detectionKnowledge",
        component: () => import("@/views/knowledgeManage/detectionKnowledge/index"),
        name: "detectionKnowledge",
        meta: { title: "活动断层探测知识库", noCache: true }
      }
    ]
  },
  {
    path: "/screenData",
    component: Layout,
    hidden: true,
    redirect: "noRedirect",
    meta: {title: "大屏关联数据管理"},
    children: [
      {
        path: "index3",
        component: () => import("@/views/screenData/index"),
        name: "reportModelManage",
        meta: { title: "大屏关联数据管理",noCache: true },
      },
    ],
  }
];

const router = createRouter({
  history: createWebHistory("hddc"),
  routes: constantRoutes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition;
    } else {
      return { top: 0 };
    }
  },
});

export default router;
